package com.lxl.xm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lxl.xm.common.ErrorCode;
import com.lxl.xm.exception.BusinessException;
import com.lxl.xm.model.dto.student_user.StudentUserRegisterRequest;
import com.lxl.xm.model.entity.StudentUser;
import com.lxl.xm.model.entity.User;
import com.lxl.xm.service.StudentUserService;
import com.lxl.xm.mapper.StudentUserMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import scala.concurrent.impl.FutureConvertersImpl;

import javax.annotation.Resource;

import static com.lxl.xm.constant.UserConstant.USER_LOGIN_STATE;

/**
* @author 86135
* @description 针对表【student_user(学生用户表)】的数据库操作Service实现
* @createDate 2024-01-31 16:32:42
*/
@Service
public class StudentUserServiceImpl extends ServiceImpl<StudentUserMapper, StudentUser>
    implements StudentUserService{

    
    @Resource
    StudentUserMapper studentUserMapper;


    /**
     * 盐值，混淆密码
     */
    private static final String SALT = "lxl";


    @Override
    public Long studentUserRegister(StudentUserRegisterRequest studentUserRegisterRequest) {
        String userName = studentUserRegisterRequest.getUserName();
        String userPassword = studentUserRegisterRequest.getUserPassword();
        //参数校验
        if (StringUtils.isAnyBlank(userName,userPassword)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        if (userName.length() < 4){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"用户昵称过短");
        }
        if (userPassword.length() < 8 ) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户密码过短");
        }
        synchronized (userName.intern()){
            //账号不能重复
//            QueryWrapper<StudentUser> accountQueryWrapper = new QueryWrapper<>();
//            accountQueryWrapper.eq("user_account",userAccount);
//            Long count = studentUserMapper.selectCount(accountQueryWrapper);
//            if (count > 0){
//                throw new BusinessException(ErrorCode.PARAMS_ERROR, "账号重复");
//            }
            //昵称不能重复
            QueryWrapper<StudentUser> nameQueryWrapper = new QueryWrapper<>();
            nameQueryWrapper.eq("user_name",userName);
            Long count = studentUserMapper.selectCount(nameQueryWrapper);
            if (count > 0){
                throw new BusinessException(ErrorCode.PARAMS_ERROR, "昵称重复");
            }
            // 2. 加密
            String encryptPassword = DigestUtils.md5DigestAsHex((SALT + userPassword).getBytes());

            // 3. 插入数据
            StudentUser studentUser = new StudentUser();
            studentUser.setUserPassword(encryptPassword);
            boolean saveResult = this.save(studentUser);
            if (!saveResult) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "注册失败，数据库错误");
            }
            return studentUser.getId();
        }
    }

    @Override
    public Boolean insertUserName(String studentNumber) {
        if (studentNumber == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"学号错误");
        }
        //学号
        StudentUser studentUser = new StudentUser();
        studentUser.setStudentNumber(studentNumber);
        return this.save(studentUser);
    }

    @Override
    public Boolean insertStudentUser(String studentNumber, String userPassword) {
        if (studentNumber == null || userPassword == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        //插入学生
        StudentUser studentUser = new StudentUser();
        studentUser.setStudentNumber(studentNumber);
        studentUser.setUserPassword(userPassword);
        return this.save(studentUser);
    }

    @Override
    public StudentUser login(String studentNumber, String studentPassword) {
        // 1. 校验
        if (StringUtils.isAnyBlank(studentNumber, studentPassword)) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
        }
        // 查询用户是否存在
        QueryWrapper<StudentUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("student_number", studentNumber);
        queryWrapper.eq("user_password", studentPassword);
        StudentUser studentUser = studentUserMapper.selectOne(queryWrapper);
        // 用户不存在
        if (studentUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "用户不存在或密码错误");
        }
        return studentUser;
    }
}




